Write a program in C++ to swap two numbers using reference variable

Assignment 8

Write a program in C++ to swap two numbers using reference variable.

Program Code with explanation

1.          #include <iostream>

2.          using namespace std;

3.          void swap(int * a, int * b)

4.          {

5.                      int temp;

6.                      temp = * a ;

7.                      * a = * b ;

8.                      * b = temp;

9.                      cout<<"Number After swap \n";

10.                   cout<<"a = "<< * a << " \t b = "<< * b << endl;

11.       }

12.       int main()

13.       {

14.                   int a, b;

15.                   cout<<" Enter two number to swap using call by value \n";

16.                   cout<<" Enter first number  ";

17.                   cin>>a;

18.                   cout<<" Enter second number  ";

19.                   cin>>b;

20.                   cout<<"Number Before swap \n ";

21.                   cout<<"a = " << a << "\t b = "<< b << endl;

22.                   swap(&a, &b);

23.                   cout<<"Display values after swap function call completed \n ";

24.                   cout<<"a = " << a << "\t b = "<< b << endl;

25.                   return 0;

26.       }

Explanation of Code:

Line No. 1 and 2: include the Library file and stander input – output functions.

Line No. 3 to 11:  User defined function of call by reference. Swap function has two arguments. Call statement of swap function at Line No 22. The function call statement contains the address of actual parameter. At function called statement, they are copied to the pointer of the formal parameter.

In swap function scope, we can swap the values using third variables. Actually, swap the variable by pointing to the variable of the main function. So swap variable in user defined function reflected in main function.

Line No. 12: main function header.

Line No. 13 and 24: Begin and end of main function respectively.

Line No. 14: Create two integer variables.

Line No. 15 to 19: Accept two integer variables using appropriate massage.

Line No. 20 to 21: Display the values of variables before swap.

Line No. 22: call swap function using two parameters. These two parameters are actual parameter. Address of actual parameter copy into pointer of formal parameter at line no 3. Change the values of formal parameter are reflected actual parameter.  

Line No. 23 and 24: Actual parameters are swap. Displayed variables to check it.

Line No. 25: return statement appropriate with main function return data type.

Program Code for run:

#include <iostream>

using namespace std;

void swap(int * a, int * b)

{

            int temp;

            temp = * a ;

            * a = * b ;

            * b = temp;

            cout<<"Number After swap \n";

            cout<<"a = "<< * a << " \t b = "<< * b << endl;

}

int main()

{

            int a, b;

            cout<<"Enter two number to swap using call by value\n";

            cout<<"Enter first number ";

            cin>>a;

            cout<<"Enter second number ";

            cin>>b;

            cout<<"Number Before swap \n";

            cout<<" a = " << a << "\t b = " << b <<endl;

            swap(&a, &b);

            cout<<"Display values after swap function call completed \n ";

            cout<<"a = " << a << "\t b = "<< b << endl;

            return 0;

}

Output of Program:

Enter two number to swap using call by value

Enter first number 10

Enter second number 20

Number Before swap

 a = 10            b = 20

Number After swap

a = 20              b = 10

Display values after swap function call completed

 a = 20            b = 10

Comments